package com.singularity.common.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import com.singularity.common.domain.entity.Role;
import com.singularity.common.domain.entity.RolePermission;
import com.singularity.common.domain.enumeration.PrefixEnum;
import com.singularity.common.mapper.RoleMapper;
import com.singularity.common.mapper.RolePermissionMapper;
import com.singularity.common.domain.dto.role.RoleInfoDTO;
import com.singularity.common.domain.dto.role.RoleListDTO;
import com.singularity.common.domain.enumeration.StatusEnum;
import com.singularity.common.domain.param.role.RoleAddParam;
import com.singularity.common.domain.param.role.RoleDistributeParam;
import com.singularity.common.domain.param.role.RoleListParam;
import com.singularity.common.domain.param.role.RoleUpdateParam;
import com.singularity.common.service.RoleService;
import com.singularity.utils.page.PageUtil;
import com.singularity.utils.page.ResponseDTOWithPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 天
 * @description: 角色接口实现
 */
@Service
public class RoleServiceImpl implements RoleService {
    private final RoleMapper roleMapper;

    private final RolePermissionMapper rolePermissionMapper;

    @Autowired
    public RoleServiceImpl(RoleMapper roleMapper, RolePermissionMapper rolePermissionMapper) {
        this.roleMapper = roleMapper;
        this.rolePermissionMapper = rolePermissionMapper;
    }

    @Override
    public List<RoleInfoDTO> getRoleInfo(Integer userId) {
        return roleMapper.selectByUserId(userId);
    }

    @Override
    public ResponseDTOWithPage<RoleListDTO> getRoleList(RoleListParam roleListParam) {
        PageUtil.startPage(roleListParam,true,RoleListDTO.class);
        List<RoleListDTO> roleList = roleMapper.getRoleList(roleListParam);
        ResponseDTOWithPage<RoleListDTO> listDTO = PageUtil.getListDTO(roleList, roleListParam);
        List<RoleListDTO> finalList = listDTO.getList().stream()
                .peek(roleListDTO -> roleListDTO.setStatus(StatusEnum.getValue(Integer.valueOf(roleListDTO.getStatus()))))
                .collect(Collectors.toList());
        return new ResponseDTOWithPage<>(finalList,listDTO.getCount());
    }

    @Override
    public int updateById(RoleUpdateParam roleUpdateParam) {
        String loginId = (String)StpUtil.getLoginId();
        Integer userId = Integer.valueOf(loginId.split(PrefixEnum.USER.getValue())[1]);
        Role role = BeanUtil.copyProperties(roleUpdateParam, Role.class);
        role.setModifyId(userId);
        return roleMapper.updateById(role);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertRole(RoleAddParam roleAddParam) {
        String loginId = (String)StpUtil.getLoginId();
        Integer userId = Integer.valueOf(loginId.split(PrefixEnum.USER.getValue())[1]);
        Role role = BeanUtil.copyProperties(roleAddParam, Role.class);
        role.setSort(0);
        role.setStatus(StatusEnum.ENABLE.getName());
        role.setCreatorId(userId);
        roleMapper.insert(role);
        List<RolePermission> rolePermissionList = roleAddParam.getPermissionIds().stream()
                .map(permissionId -> new RolePermission(role.getId(), permissionId, userId))
                .collect(Collectors.toList());
        int insertBatch = rolePermissionMapper.insertBatch(rolePermissionList);
        return insertBatch == rolePermissionList.size();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean distribute(RoleDistributeParam roleDistributeParam) {
        String loginId = (String)StpUtil.getLoginId();
        Integer userId = Integer.valueOf(loginId.split(PrefixEnum.USER.getValue())[1]);
        Integer roleId = roleDistributeParam.getRoleId();
        // 将原有权限删除
        LambdaUpdateWrapper<RolePermission> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RolePermission::getRoleId,roleId);
        rolePermissionMapper.delete(updateWrapper);
        // 添加新权限
        List<RolePermission> rolePermissionList = roleDistributeParam.getPermissionIds().stream()
                .map(permissionId -> new RolePermission(roleId, permissionId, userId))
                .collect(Collectors.toList());
        int insertBatch = rolePermissionMapper.insertBatch(rolePermissionList);
        return insertBatch == rolePermissionList.size();
    }

}
